import store from "../store";
import {Notify} from "vant";

let audio = new Audio();

// 监听播放
audio.addEventListener("play",()=>{
    store.commit("music/setMusicIsPlay",true);
});
// 监听暂停
audio.addEventListener("pause",()=>{
    store.commit("music/setMusicIsPlay",false);
});
audio.addEventListener("ended",()=>{
    store.dispatch("music/next");
})
// 监听错误
audio.addEventListener("error",(...err)=>{
    Notify({
        type:'danger',
        message:"音乐无法播放！"
    });
    store.commit("music/setMusicLoading",false);
    store.commit("music/setMusicIsPlay",false);
});
// audio.addEventListener("loadstart",()=>{
//     store.commit("music/setMusicLoading",true);
// });
// audio.addEventListener("loadeddata",()=>{
//     store.commit("music/setMusicLoading",false);
// });

audio.addEventListener("timeupdate",(...e)=>{
    let currentTime = parseInt(audio.currentTime * 1000);
    store.commit("music/setMusicCurrentTime",currentTime);
});
export default class AudioManger{
    static play(){
        let p = audio.play();
        // p.catch(()=>{
        //     Notify({
        //         type:'danger',
        //         message:"音乐播放错误！"
        //     });
        // })
        return p;
    }
    static pause(){
        audio.pause();
    }
    static getDom(){
        return audio;
    }
    static setSrc(url){
        audio.src = url;
    }
    static getSrc(){
        return audio.src;
    }
    static setCurrentTime(time){
        audio.currentTime = time;
    }
    static getCurrentTime(){
        return audio.currentTime;
    }
}